import React, { forwardRef, useImperativeHandle, useState } from 'react';

import { Button, DatePicker, Form, Input, Modal, Radio, Tag, Select, InputNumber, message, Row, Col } from 'antd';
import { modalLayoutLg, modalLayoutMd, modalLayoutSm } from '@/config/layout';
import { request } from 'umi';
const DepartmentModal = (props: any, refs: any) => {
  const [form] = Form.useForm();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;
  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);

  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    props.callback && props.callback();
  };
  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        Obj = {
          ...Obj,

        };

        const url = code ? "/sys/department/edit" : "/sys/department/add";
        if (code) {
          Obj.id = code;
        } else {
        }
        request(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          data: Obj
        }).then((res) => {
          message.success(res?.meta?.msg);
          setIsModalVisible(false);
          handleCancel();
        })
      })
      .catch((info) => { });
  };

  const getDetail = (id: any) => {
    request("/sys/department/queryById?id=" + id, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        id
      }
    }).then((res) => {
      setFieldsValue(res?.data);
      showModal();
    })


  };
  //负责人列表
  const [userListEnum, setUserListEnum] = useState([]);
  //查询负责人
  const getUserList = () => {
    request('/sys/dictUtils/findUserList', {
      method: 'POST',
      data: {},
    }).then((res) => {
      const newAllList = [];
      let list = res?.data || [];
      list.map((e, i) => {
        newAllList.push({
          value: e.id,
          label: e.name,
        });
      });
      setUserListEnum(newAllList);
    });
  };
  const filterOption = (inputValue, option) => {
    return option.label.indexOf(inputValue) != -1
  }
  useImperativeHandle(refs, () => ({
    init: (id: any, record: any, pid: any) => {
      if (id) {
        setCode(id);
        getDetail(id)

      } else {
        resetFields();
        showModal();
      }
      getUserList();
      if (pid) {
        setFieldsValue({
          'parentId': pid
        })
      }
      // setfileList([]);
    },
  }));

  return (
    <>
      <Modal
        title={code === '' ? '新增' : '编辑'}
        visible={isModalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        centered
        width={720}
        maskClosable={false}
        bodyStyle={{
          maxHeight: window.innerHeight - 150,
          overflowY: 'auto',
        }}
      >
        <Form form={form}  {...modalLayoutMd}>
          <Form.Item label="机构编号" style={{ display: 'none' }} name="code"

          >
            <Input disabled />
          </Form.Item>
          <Form.Item label="部门名称" name="name"
            rules={[{ required: true, message: '请输入部门名称!' }]}
          >
            <Input />
          </Form.Item >
          <Form.Item label="父部门" name="parentId" style={{ display: 'none' }}
          >
            <Input />
          </Form.Item>
          <Form.Item label="部门类型" name="type"
            rules={[{ required: true, message: '请输入部门类型；字典类型：exec_type；1-内部；2-外部!' }]}
          >
            <Select>
              <Option value='1'>内部</Option>
              <Option value='2'>外部</Option>
            </Select>
          </Form.Item>
          <Form.Item label="部门负责人" name="departmentUserId"
            rules={[{ message: '请选择负责人!' }]}
          >
            <Select
              showSearch
              placeholder="请选择"
              filterOption={filterOption}
              options={userListEnum}
              allowClear={false}
              style={{ width: '100%' }}
            >
            </Select>

          </Form.Item>
        </Form>
      </Modal>
    </>
  );
};
const AddMCom = forwardRef(DepartmentModal);

export default AddMCom;
